package com.lab.tools.ui;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cuitrip.service.R;
import com.lab.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ak on 3/21/16.
 */
public class LogDetailFragment extends Fragment {


    private View mContentView;

    private HttpContent httpContent;

    @Bind(R.id.content)
    TextView content;

    @Bind(R.id.title)
    TextView title;

    public static final String DATA_KEY = "data";


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        httpContent = (HttpContent) bundle.getSerializable(DATA_KEY);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.log_detail_view, container, false);
        ButterKnife.bind(this, mContentView);
        initData();
        return mContentView;
    }

    private void initData() {
        title.setText(httpContent.getApiName());
        try {
            JSONObject json = new JSONObject(httpContent.getContent()); // Convert text to object
            String prettyJson = json.toString(4);
            content.setText(prettyJson);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


    @OnClick(R.id.back_press)
    void backPress() {
        getActivity().onBackPressed();
    }

    @OnClick(R.id.copy)
    void copyAction() {
        ClipboardManager clipboardManager = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData data = ClipData.newPlainText("text", content.getText().toString());
        clipboardManager.setPrimaryClip(data);
        ToastUtil.toast("copied");
    }

}
